package cn.qicaiwang.open.admin.controller.crm;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.StrUtil;
import cn.qicaiwang.open.common.constant.Constants;
import cn.qicaiwang.open.common.enums.ResultStatusCodeEnum;
import cn.qicaiwang.open.common.req.CrmMessageREQ;
import cn.qicaiwang.open.common.response.ResponseResult;
import cn.qicaiwang.open.common.utils.SecurityUtil;
import cn.qicaiwang.open.common.vo.CrmMessageVO;
import cn.qicaiwang.open.common.vo.CrmSessionVO;
import cn.qicaiwang.open.websocket.service.IChatContactService;
import cn.qicaiwang.open.websocket.service.ICrmMessageService;
import cn.qicaiwang.open.websocket.service.ICrmSessionService;
import cn.qicaiwang.open.websocket.vo.ChatContactVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
/**​
 * 聊天系统控制器
 */
@RestController
@RequestMapping("/crm/chat")
public class AdminChatController {

        @Autowired
        private IChatContactService chatContactsService;  // 通讯录服务

        @Autowired
        private ICrmSessionService crmSessionService;  // 会话服务

        @Autowired
        private ICrmMessageService crmMessageService;  // 消息服务
    /**
     * 通讯录
     * @param keywords
     * @return
     */
    @GetMapping("contacts")
    public ResponseResult contacts(String keywords){
        List<ChatContactVO> list = chatContactsService.contacts(keywords);
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        treeNodeConfig.setWeightKey("name");
        List<Tree<String>> trees = TreeUtil.build(list, Constants.UNIT_ROOT_ID, treeNodeConfig, (treeNode, tree) -> {
            tree.setId(treeNode.getId());
            tree.setParentId(treeNode.getParentId());
            tree.setName(treeNode.getName());
            tree.setWeight(treeNode.getName());
            tree.putExtra("type", treeNode.getType());
            tree.putExtra("avatar", treeNode.getAvatar());
            tree.putExtra("gender", treeNode.getGender());
            tree.putExtra("email", treeNode.getEmail());
            tree.putExtra("mobile", treeNode.getMobile());
            tree.putExtra("realName", treeNode.getRealName());
        });
        return ResponseResult.ok(trees);
    }

    /**
     * 聊天会话
     * @param page
     * @return
     */
    @GetMapping("sessions")
    public ResponseResult session(Page page){
        String userId = SecurityUtil.getUserId();
        Page<CrmSessionVO> sessions = crmSessionService.pagelist(page, userId);
        return ResponseResult.ok(sessions);
    }

    /**
     * 聊天会话
     * @return
     */
    @GetMapping("session/{fromUserId}/{toUserId}")
    public ResponseResult session(@PathVariable String fromUserId, @PathVariable String toUserId){
        if(StrUtil.isBlank(fromUserId) || StrUtil.isBlank(toUserId)){
            return ResponseResult.error(ResultStatusCodeEnum.PARAM_REQUIRE_ERROR);
        }
        CrmSessionVO sessions = crmSessionService.check(fromUserId, toUserId);
        return ResponseResult.ok(sessions);
    }

    @GetMapping("session/delete/{id}")
    public ResponseResult delete(@PathVariable String id){
        if(StrUtil.isBlank(id)){
            return ResponseResult.error(ResultStatusCodeEnum.PARAM_REQUIRE_ERROR);
        }
        boolean b = crmSessionService.delete(id);
        if(b){
            return ResponseResult.ok();
        }
        return ResponseResult.error(ResultStatusCodeEnum.HTTP_ERROR);
    }

    /**
     * 聊天记录
     * @param page
     * @param req
     * @return
     */
    @GetMapping("history")
    public ResponseResult session(Page page, CrmMessageREQ req){
        if(StrUtil.isBlank(req.getSessionId())){
            return ResponseResult.error(ResultStatusCodeEnum.PARAM_REQUIRE_ERROR);
        }
        Page<CrmMessageVO> history = crmMessageService.pagelist(page, req);
        return ResponseResult.ok(history);
    }
}

